{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### Answers to Assignment 5 - Regular Expressions\n",
    "\n",
    "Complete the tasks below. Please turn in a single Jupyter notebook named `5_first_last.ipynb` (substitute your first and last name). Please run Kernel > Restart & Run All on your notebook before turning in."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Answer to A. Regular expressions in the command line\n",
    "\n",
    "Setup: Download [`scripps_pier_20151110.csv`](https://github.com/cuttlefishh/python-for-data-analysis/blob/master/data/scripps_pier_20151110.csv) to your directory `~/sio209/data`, if you haven't already. You will use regular expressions to get information from the file.\n",
    "\n",
    "1. Get all the rows with data from 11/10/15 using grep. How would you keep the header line also? (Hint: to get an \"or\", use `\\|` in grep or `|` in egrep.) \n",
    "* Get all the rows where the third column (pressure) begins with a \"3\" using grep. (Hint: if you want to match a period `.` and not any character, you can escape it with `\\.`.)\n",
    "* Change the year \"15\" to \"2015\" using sed. (Reminder: sed syntax is `'sed s/find/replace/'`. Hint: if you want to match a forward slash in sed, you can escape it with a backslash like `\\/`.)\n",
    "* Copy your shell code (e.g., grep, sed) to a Markdown cell in your notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11/10/15 1:42,22.307,3.712,33.199,19.95\r\n",
      "11/10/15 1:35,22.311,3.588,33.201,19.94\r\n",
      "11/10/15 1:29,22.305,3.541,33.2,19.95\r\n",
      "11/10/15 1:23,22.323,3.463,33.2,19.95\r\n",
      "11/10/15 1:17,22.316,3.471,33.199,19.95\r\n",
      "11/10/15 1:11,22.315,3.476,33.198,19.95\r\n",
      "11/10/15 1:05,22.31,3.448,33.199,19.96\r\n",
      "11/10/15 0:59,22.316,3.377,33.2,19.99\r\n",
      "11/10/15 0:53,22.311,3.338,33.2,20\r\n",
      "11/10/15 0:47,22.322,3.325,33.201,20.01\r\n",
      "11/10/15 0:41,22.311,3.344,33.2,20.04\r\n",
      "11/10/15 0:35,22.311,3.217,33.201,20.05\r\n",
      "11/10/15 0:29,22.307,3.265,33.199,20.08\r\n",
      "11/10/15 0:23,22.32,3.22,33.197,20.09\r\n",
      "11/10/15 0:17,22.322,3.211,33.199,20.08\r\n",
      "11/10/15 0:11,22.323,3.134,33.197,20.09\r\n",
      "11/10/15 0:05,22.315,3.018,33.197,20.09\r\n"
     ]
    }
   ],
   "source": [
    "!cat ../../data/scripps_pier_20151110.csv | grep \"11/10/15\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Date,chl (ug/L),pres (dbar),sal (PSU),temp (C)\r\n",
      "11/10/15 1:42,22.307,3.712,33.199,19.95\r\n",
      "11/10/15 1:35,22.311,3.588,33.201,19.94\r\n",
      "11/10/15 1:29,22.305,3.541,33.2,19.95\r\n",
      "11/10/15 1:23,22.323,3.463,33.2,19.95\r\n",
      "11/10/15 1:17,22.316,3.471,33.199,19.95\r\n",
      "11/10/15 1:11,22.315,3.476,33.198,19.95\r\n",
      "11/10/15 1:05,22.31,3.448,33.199,19.96\r\n",
      "11/10/15 0:59,22.316,3.377,33.2,19.99\r\n",
      "11/10/15 0:53,22.311,3.338,33.2,20\r\n",
      "11/10/15 0:47,22.322,3.325,33.201,20.01\r\n",
      "11/10/15 0:41,22.311,3.344,33.2,20.04\r\n",
      "11/10/15 0:35,22.311,3.217,33.201,20.05\r\n",
      "11/10/15 0:29,22.307,3.265,33.199,20.08\r\n",
      "11/10/15 0:23,22.32,3.22,33.197,20.09\r\n",
      "11/10/15 0:17,22.322,3.211,33.199,20.08\r\n",
      "11/10/15 0:11,22.323,3.134,33.197,20.09\r\n",
      "11/10/15 0:05,22.315,3.018,33.197,20.09\r\n"
     ]
    }
   ],
   "source": [
    "!cat ../../data/scripps_pier_20151110.csv | egrep \"11/10/15|Date\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11/10/15 1:42,22.307,3.712,33.199,19.95\r\n",
      "11/10/15 1:35,22.311,3.588,33.201,19.94\r\n",
      "11/10/15 1:29,22.305,3.541,33.2,19.95\r\n",
      "11/10/15 1:23,22.323,3.463,33.2,19.95\r\n",
      "11/10/15 1:17,22.316,3.471,33.199,19.95\r\n",
      "11/10/15 1:11,22.315,3.476,33.198,19.95\r\n",
      "11/10/15 1:05,22.31,3.448,33.199,19.96\r\n",
      "11/10/15 0:59,22.316,3.377,33.2,19.99\r\n",
      "11/10/15 0:53,22.311,3.338,33.2,20\r\n",
      "11/10/15 0:47,22.322,3.325,33.201,20.01\r\n",
      "11/10/15 0:41,22.311,3.344,33.2,20.04\r\n",
      "11/10/15 0:35,22.311,3.217,33.201,20.05\r\n",
      "11/10/15 0:29,22.307,3.265,33.199,20.08\r\n",
      "11/10/15 0:23,22.32,3.22,33.197,20.09\r\n",
      "11/10/15 0:17,22.322,3.211,33.199,20.08\r\n",
      "11/10/15 0:11,22.323,3.134,33.197,20.09\r\n",
      "11/10/15 0:05,22.315,3.018,33.197,20.09\r\n",
      "11/9/15 23:59,22.319,3.031,33.198,20.08\r\n",
      "11/9/15 23:53,22.327,3.118,33.199,20.11\r\n",
      "11/9/15 23:41,22.328,3.014,33.198,20.16\r\n",
      "11/9/15 23:29,22.336,3.013,33.194,20.17\r\n",
      "11/9/15 23:23,22.337,3.011,33.194,20.16\r\n",
      "11/9/15 20:17,22.402,3.061,33.206,20.07\r\n",
      "11/9/15 20:05,22.409,3.065,33.204,20.05\r\n",
      "11/9/15 19:59,22.412,3.159,33.203,20.04\r\n",
      "11/9/15 19:53,22.418,3.039,33.206,20.04\r\n",
      "11/9/15 19:47,22.422,3.163,33.204,20.04\r\n",
      "11/9/15 19:41,22.423,3.193,33.206,20.03\r\n",
      "11/9/15 19:34,22.423,3.204,33.202,20.05\r\n",
      "11/9/15 19:28,22.413,3.254,33.2,20.02\r\n",
      "11/9/15 19:22,22.418,3.316,33.202,19.96\r\n",
      "11/9/15 19:16,22.41,3.209,33.2,19.96\r\n",
      "11/9/15 19:10,22.426,3.328,33.203,19.95\r\n"
     ]
    }
   ],
   "source": [
    "!cat ../../data/scripps_pier_20151110.csv | grep \".*,.*,3\\..*,.*,.*\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Date,chl (ug/L),pres (dbar),sal (PSU),temp (C)\r\n",
      "11/10/2015 1:42,22.307,3.712,33.199,19.95\r\n",
      "11/10/2015 1:35,22.311,3.588,33.201,19.94\r\n",
      "11/10/2015 1:29,22.305,3.541,33.2,19.95\r\n",
      "11/10/2015 1:23,22.323,3.463,33.2,19.95\r\n",
      "11/10/2015 1:17,22.316,3.471,33.199,19.95\r\n",
      "11/10/2015 1:11,22.315,3.476,33.198,19.95\r\n",
      "11/10/2015 1:05,22.31,3.448,33.199,19.96\r\n",
      "11/10/2015 0:59,22.316,3.377,33.2,19.99\r\n",
      "11/10/2015 0:53,22.311,3.338,33.2,20\r\n",
      "11/10/2015 0:47,22.322,3.325,33.201,20.01\r\n",
      "11/10/2015 0:41,22.311,3.344,33.2,20.04\r\n",
      "11/10/2015 0:35,22.311,3.217,33.201,20.05\r\n",
      "11/10/2015 0:29,22.307,3.265,33.199,20.08\r\n",
      "11/10/2015 0:23,22.32,3.22,33.197,20.09\r\n",
      "11/10/2015 0:17,22.322,3.211,33.199,20.08\r\n",
      "11/10/2015 0:11,22.323,3.134,33.197,20.09\r\n",
      "11/10/2015 0:05,22.315,3.018,33.197,20.09\r\n",
      "11/9/2015 23:59,22.319,3.031,33.198,20.08\r\n",
      "11/9/2015 23:53,22.327,3.118,33.199,20.11\r\n",
      "11/9/2015 23:47,22.332,2.962,33.199,20.13\r\n",
      "11/9/2015 23:41,22.328,3.014,33.198,20.16\r\n",
      "11/9/2015 23:35,22.341,2.972,33.193,20.18\r\n",
      "11/9/2015 23:29,22.336,3.013,33.194,20.17\r\n",
      "11/9/2015 23:23,22.337,3.011,33.194,20.16\r\n",
      "11/9/2015 23:17,22.334,2.875,33.194,20.16\r\n",
      "11/9/2015 23:11,22.337,2.809,33.194,20.16\r\n",
      "11/9/2015 23:05,22.335,2.868,33.195,20.18\r\n",
      "11/9/2015 22:59,22.33,2.851,33.197,20.18\r\n",
      "11/9/2015 22:53,22.315,2.769,33.199,20.18\r\n",
      "11/9/2015 22:47,22.318,2.774,33.191,20.19\r\n",
      "11/9/2015 22:41,22.318,2.875,33.191,20.15\r\n",
      "11/9/2015 22:35,22.32,2.752,33.192,20.15\r\n",
      "11/9/2015 22:29,22.319,2.814,33.186,20.16\r\n",
      "11/9/2015 22:23,22.328,2.781,33.184,20.13\r\n",
      "11/9/2015 22:17,22.31,2.794,33.189,20.11\r\n",
      "11/9/2015 22:11,22.32,2.776,33.197,20.08\r\n",
      "11/9/2015 22:05,22.325,2.77,33.199,20.07\r\n",
      "11/9/2015 21:59,22.32,2.76,33.198,20.08\r\n",
      "11/9/2015 21:53,22.335,2.798,33.198,20.08\r\n",
      "11/9/2015 21:47,22.333,2.764,33.198,20.08\r\n",
      "11/9/2015 21:41,22.343,2.714,33.197,20.09\r\n",
      "11/9/2015 21:35,22.351,2.821,33.195,20.06\r\n",
      "11/9/2015 21:29,22.359,2.832,33.201,20.05\r\n",
      "11/9/2015 21:23,22.363,2.735,33.203,20.05\r\n",
      "11/9/2015 21:17,22.363,2.746,33.205,20.05\r\n",
      "11/9/2015 21:11,22.368,2.76,33.204,20.05\r\n",
      "11/9/2015 21:05,22.37,2.805,33.205,20.05\r\n",
      "11/9/2015 20:59,22.377,2.813,33.205,20.05\r\n",
      "11/9/2015 20:53,22.395,2.913,33.206,20.05\r\n",
      "11/9/2015 20:47,22.395,2.818,33.206,20.05\r\n",
      "11/9/2015 20:41,22.379,2.877,33.205,20.07\r\n",
      "11/9/2015 20:35,22.388,2.921,33.206,20.07\r\n",
      "11/9/2015 20:29,22.387,2.971,33.205,20.07\r\n",
      "11/9/2015 20:23,22.392,2.983,33.206,20.07\r\n",
      "11/9/2015 20:17,22.402,3.061,33.206,20.07\r\n",
      "11/9/2015 20:11,22.404,2.961,33.206,20.08\r\n",
      "11/9/2015 20:05,22.409,3.065,33.204,20.05\r\n",
      "11/9/2015 19:59,22.412,3.159,33.203,20.04\r\n",
      "11/9/2015 19:53,22.418,3.039,33.206,20.04\r\n",
      "11/9/2015 19:47,22.422,3.163,33.204,20.04\r\n",
      "11/9/2015 19:41,22.423,3.193,33.206,20.03\r\n",
      "11/9/2015 19:34,22.423,3.204,33.202,20.05\r\n",
      "11/9/2015 19:28,22.413,3.254,33.2,20.02\r\n",
      "11/9/2015 19:22,22.418,3.316,33.202,19.96\r\n",
      "11/9/2015 19:16,22.41,3.209,33.2,19.96\r\n",
      "11/9/2015 19:10,22.426,3.328,33.203,19.95\r\n"
     ]
    }
   ],
   "source": [
    "!cat ../../data/scripps_pier_20151110.csv | sed 's/\\/15/\\/2015/'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Answer to B. Regular expressions in Python\n",
    "\n",
    "Setup: Import the `re` module.\n",
    "\n",
    "1. Create a string variable with the value 'The quick brown fox jumps over the lazy dog'.\n",
    "2. Use `re.match` or `re.search` to see if the string starts with 'The'.\n",
    "3. Use `re.search` to find the first instance of a lowercase letter followed by an 'o'.\n",
    "4. Use `re.findall` to find all instances of a lowercase letter followed by an 'o'.\n",
    "5. Use `re.sub` to change the last word to an animal of your choice."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = 'The quick brown fox jumps over the lazy dog'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matchobj = re.match(r'The', s)\n",
    "matchobj.group()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ro'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "searchobj = re.search(r'[a-z]o', s)\n",
    "searchobj.group()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['ro', 'fo', 'do']"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "findobj = re.findall(r'[a-z]o', s)\n",
    "findobj"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The quick brown fox jumps over the lazy whalewhale'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.sub(r'\\w*$', 'whale', s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Answer to C. Numpy basics\n",
    "\n",
    "1. Create an array of ten random floats ranging from 0 to 10. Create a second array of ten integers from 0 to 9. \n",
    "3. Use 'fancy indexing' with masks to find the values in your first array that are greater than their corresponding values in the second array (e.g., if the first element in array 1 is greater than the first element in array 2)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.random.rand(10) * 10\n",
    "y = np.arange(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5.38492139, 7.30115743, 7.9164117 , 5.17483359, 9.02989914,\n",
       "       2.46943523, 6.06048994, 5.79185622, 7.42187875, 6.165999  ])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True,  True,  True,  True, False,  True, False, False,\n",
       "       False])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x > y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5.38492139, 7.30115743, 7.9164117 , 5.17483359, 9.02989914,\n",
       "       6.06048994])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[x > y]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Answer to D. Pandas basics\n",
    "\n",
    "1. Read in the csv file `scripps_pier_20151110.csv` as a Pandas DataFrame with the index as the Date column and the column headers from the first row of that file.\n",
    "2. Print all the date/times from 11/10/15. (Hint: you can get the indexes from the DataFrame, convert it to a list, then iterate over that list (using a for loop or list comprehension), and then use `re.match` or `re.search`.)\n",
    "3. Print all the rows from 11/10/15. (Hint: you can use your answer to the exercise you just did but use `.loc` indexing.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Option 1 - Use a regular expression to search the date/times"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('../../data/scripps_pier_20151110.csv', index_col=0, header=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "l = list(df.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['11/10/15 1:42',\n",
       " '11/10/15 1:35',\n",
       " '11/10/15 1:29',\n",
       " '11/10/15 1:23',\n",
       " '11/10/15 1:17',\n",
       " '11/10/15 1:11',\n",
       " '11/10/15 1:05',\n",
       " '11/10/15 0:59',\n",
       " '11/10/15 0:53',\n",
       " '11/10/15 0:47',\n",
       " '11/10/15 0:41',\n",
       " '11/10/15 0:35',\n",
       " '11/10/15 0:29',\n",
       " '11/10/15 0:23',\n",
       " '11/10/15 0:17',\n",
       " '11/10/15 0:11',\n",
       " '11/10/15 0:05']"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[date for date in l if re.match(r'11/10/15', date)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>chl (ug/L)</th>\n",
       "      <th>pres (dbar)</th>\n",
       "      <th>sal (PSU)</th>\n",
       "      <th>temp (C)</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>11/10/15 1:42</th>\n",
       "      <td>22.307</td>\n",
       "      <td>3.712</td>\n",
       "      <td>33.199</td>\n",
       "      <td>19.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11/10/15 1:35</th>\n",
       "      <td>22.311</td>\n",
       "      <td>3.588</td>\n",
       "      <td>33.201</td>\n",
       "      <td>19.94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11/10/15 1:29</th>\n",
       "      <td>22.305</td>\n",
       "      <td>3.541</td>\n",
       "      <td>33.200</td>\n",
       "      <td>19.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11/10/15 1:23</th>\n",
       "      <td>22.323</td>\n",
       "      <td>3.463</td>\n",
       "      <td>33.200</td>\n",
       "      <td>19.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11/10/15 1:17</th>\n",
       "      <td>22.316</td>\n",
       "      <td>3.471</td>\n",
       "      <td>33.199</td>\n",
       "      <td>19.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11/10/15 1:11</th>\n",
       "      <td>22.315</td>\n",
       "      <td>3.476</td>\n",
       "      <td>33.198</td>\n",
       "      <td>19.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11/10/15 1:05</th>\n",
       "      <td>22.310</td>\n",
       "      <td>3.448</td>\n",
       "      <td>33.199</td>\n",
       "      <td>19.96</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11/10/15 0:59</th>\n",
       "      <td>22.316</td>\n",
       "      <td>3.377</td>\n",
       "      <td>33.200</td>\n",
       "      <td>19.99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11/10/15 0:53</th>\n",
       "      <td>22.311</td>\n",
       "      <td>3.338</td>\n",
       "      <td>33.200</td>\n",
       "      <td>20.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11/10/15 0:47</th>\n",
       "      <td>22.322</td>\n",
       "      <td>3.325</td>\n",
       "      <td>33.201</td>\n",
       "      <td>20.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11/10/15 0:41</th>\n",
       "      <td>22.311</td>\n",
       "      <td>3.344</td>\n",
       "      <td>33.200</td>\n",
       "      <td>20.04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11/10/15 0:35</th>\n",
       "      <td>22.311</td>\n",
       "      <td>3.217</td>\n",
       "      <td>33.201</td>\n",
       "      <td>20.05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11/10/15 0:29</th>\n",
       "      <td>22.307</td>\n",
       "      <td>3.265</td>\n",
       "      <td>33.199</td>\n",
       "      <td>20.08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11/10/15 0:23</th>\n",
       "      <td>22.320</td>\n",
       "      <td>3.220</td>\n",
       "      <td>33.197</td>\n",
       "      <td>20.09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11/10/15 0:17</th>\n",
       "      <td>22.322</td>\n",
       "      <td>3.211</td>\n",
       "      <td>33.199</td>\n",
       "      <td>20.08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11/10/15 0:11</th>\n",
       "      <td>22.323</td>\n",
       "      <td>3.134</td>\n",
       "      <td>33.197</td>\n",
       "      <td>20.09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11/10/15 0:05</th>\n",
       "      <td>22.315</td>\n",
       "      <td>3.018</td>\n",
       "      <td>33.197</td>\n",
       "      <td>20.09</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               chl (ug/L)  pres (dbar)  sal (PSU)  temp (C)\n",
       "Date                                                       \n",
       "11/10/15 1:42      22.307        3.712     33.199     19.95\n",
       "11/10/15 1:35      22.311        3.588     33.201     19.94\n",
       "11/10/15 1:29      22.305        3.541     33.200     19.95\n",
       "11/10/15 1:23      22.323        3.463     33.200     19.95\n",
       "11/10/15 1:17      22.316        3.471     33.199     19.95\n",
       "11/10/15 1:11      22.315        3.476     33.198     19.95\n",
       "11/10/15 1:05      22.310        3.448     33.199     19.96\n",
       "11/10/15 0:59      22.316        3.377     33.200     19.99\n",
       "11/10/15 0:53      22.311        3.338     33.200     20.00\n",
       "11/10/15 0:47      22.322        3.325     33.201     20.01\n",
       "11/10/15 0:41      22.311        3.344     33.200     20.04\n",
       "11/10/15 0:35      22.311        3.217     33.201     20.05\n",
       "11/10/15 0:29      22.307        3.265     33.199     20.08\n",
       "11/10/15 0:23      22.320        3.220     33.197     20.09\n",
       "11/10/15 0:17      22.322        3.211     33.199     20.08\n",
       "11/10/15 0:11      22.323        3.134     33.197     20.09\n",
       "11/10/15 0:05      22.315        3.018     33.197     20.09"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[[date for date in l if re.match(r'11/10/15', date)]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Option 2 - Convert the index to a datetime index, then just index the date (works with or without .loc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.index = pd.to_datetime(df.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>chl (ug/L)</th>\n",
       "      <th>pres (dbar)</th>\n",
       "      <th>sal (PSU)</th>\n",
       "      <th>temp (C)</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-11-10 01:42:00</th>\n",
       "      <td>22.307</td>\n",
       "      <td>3.712</td>\n",
       "      <td>33.199</td>\n",
       "      <td>19.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 01:35:00</th>\n",
       "      <td>22.311</td>\n",
       "      <td>3.588</td>\n",
       "      <td>33.201</td>\n",
       "      <td>19.94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 01:29:00</th>\n",
       "      <td>22.305</td>\n",
       "      <td>3.541</td>\n",
       "      <td>33.200</td>\n",
       "      <td>19.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 01:23:00</th>\n",
       "      <td>22.323</td>\n",
       "      <td>3.463</td>\n",
       "      <td>33.200</td>\n",
       "      <td>19.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 01:17:00</th>\n",
       "      <td>22.316</td>\n",
       "      <td>3.471</td>\n",
       "      <td>33.199</td>\n",
       "      <td>19.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 01:11:00</th>\n",
       "      <td>22.315</td>\n",
       "      <td>3.476</td>\n",
       "      <td>33.198</td>\n",
       "      <td>19.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 01:05:00</th>\n",
       "      <td>22.310</td>\n",
       "      <td>3.448</td>\n",
       "      <td>33.199</td>\n",
       "      <td>19.96</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 00:59:00</th>\n",
       "      <td>22.316</td>\n",
       "      <td>3.377</td>\n",
       "      <td>33.200</td>\n",
       "      <td>19.99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 00:53:00</th>\n",
       "      <td>22.311</td>\n",
       "      <td>3.338</td>\n",
       "      <td>33.200</td>\n",
       "      <td>20.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 00:47:00</th>\n",
       "      <td>22.322</td>\n",
       "      <td>3.325</td>\n",
       "      <td>33.201</td>\n",
       "      <td>20.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 00:41:00</th>\n",
       "      <td>22.311</td>\n",
       "      <td>3.344</td>\n",
       "      <td>33.200</td>\n",
       "      <td>20.04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 00:35:00</th>\n",
       "      <td>22.311</td>\n",
       "      <td>3.217</td>\n",
       "      <td>33.201</td>\n",
       "      <td>20.05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 00:29:00</th>\n",
       "      <td>22.307</td>\n",
       "      <td>3.265</td>\n",
       "      <td>33.199</td>\n",
       "      <td>20.08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 00:23:00</th>\n",
       "      <td>22.320</td>\n",
       "      <td>3.220</td>\n",
       "      <td>33.197</td>\n",
       "      <td>20.09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 00:17:00</th>\n",
       "      <td>22.322</td>\n",
       "      <td>3.211</td>\n",
       "      <td>33.199</td>\n",
       "      <td>20.08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 00:11:00</th>\n",
       "      <td>22.323</td>\n",
       "      <td>3.134</td>\n",
       "      <td>33.197</td>\n",
       "      <td>20.09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 00:05:00</th>\n",
       "      <td>22.315</td>\n",
       "      <td>3.018</td>\n",
       "      <td>33.197</td>\n",
       "      <td>20.09</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     chl (ug/L)  pres (dbar)  sal (PSU)  temp (C)\n",
       "Date                                                             \n",
       "2015-11-10 01:42:00      22.307        3.712     33.199     19.95\n",
       "2015-11-10 01:35:00      22.311        3.588     33.201     19.94\n",
       "2015-11-10 01:29:00      22.305        3.541     33.200     19.95\n",
       "2015-11-10 01:23:00      22.323        3.463     33.200     19.95\n",
       "2015-11-10 01:17:00      22.316        3.471     33.199     19.95\n",
       "2015-11-10 01:11:00      22.315        3.476     33.198     19.95\n",
       "2015-11-10 01:05:00      22.310        3.448     33.199     19.96\n",
       "2015-11-10 00:59:00      22.316        3.377     33.200     19.99\n",
       "2015-11-10 00:53:00      22.311        3.338     33.200     20.00\n",
       "2015-11-10 00:47:00      22.322        3.325     33.201     20.01\n",
       "2015-11-10 00:41:00      22.311        3.344     33.200     20.04\n",
       "2015-11-10 00:35:00      22.311        3.217     33.201     20.05\n",
       "2015-11-10 00:29:00      22.307        3.265     33.199     20.08\n",
       "2015-11-10 00:23:00      22.320        3.220     33.197     20.09\n",
       "2015-11-10 00:17:00      22.322        3.211     33.199     20.08\n",
       "2015-11-10 00:11:00      22.323        3.134     33.197     20.09\n",
       "2015-11-10 00:05:00      22.315        3.018     33.197     20.09"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['11/10/15']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>chl (ug/L)</th>\n",
       "      <th>pres (dbar)</th>\n",
       "      <th>sal (PSU)</th>\n",
       "      <th>temp (C)</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-11-10 01:42:00</th>\n",
       "      <td>22.307</td>\n",
       "      <td>3.712</td>\n",
       "      <td>33.199</td>\n",
       "      <td>19.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 01:35:00</th>\n",
       "      <td>22.311</td>\n",
       "      <td>3.588</td>\n",
       "      <td>33.201</td>\n",
       "      <td>19.94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 01:29:00</th>\n",
       "      <td>22.305</td>\n",
       "      <td>3.541</td>\n",
       "      <td>33.200</td>\n",
       "      <td>19.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 01:23:00</th>\n",
       "      <td>22.323</td>\n",
       "      <td>3.463</td>\n",
       "      <td>33.200</td>\n",
       "      <td>19.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 01:17:00</th>\n",
       "      <td>22.316</td>\n",
       "      <td>3.471</td>\n",
       "      <td>33.199</td>\n",
       "      <td>19.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 01:11:00</th>\n",
       "      <td>22.315</td>\n",
       "      <td>3.476</td>\n",
       "      <td>33.198</td>\n",
       "      <td>19.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 01:05:00</th>\n",
       "      <td>22.310</td>\n",
       "      <td>3.448</td>\n",
       "      <td>33.199</td>\n",
       "      <td>19.96</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 00:59:00</th>\n",
       "      <td>22.316</td>\n",
       "      <td>3.377</td>\n",
       "      <td>33.200</td>\n",
       "      <td>19.99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 00:53:00</th>\n",
       "      <td>22.311</td>\n",
       "      <td>3.338</td>\n",
       "      <td>33.200</td>\n",
       "      <td>20.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 00:47:00</th>\n",
       "      <td>22.322</td>\n",
       "      <td>3.325</td>\n",
       "      <td>33.201</td>\n",
       "      <td>20.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 00:41:00</th>\n",
       "      <td>22.311</td>\n",
       "      <td>3.344</td>\n",
       "      <td>33.200</td>\n",
       "      <td>20.04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 00:35:00</th>\n",
       "      <td>22.311</td>\n",
       "      <td>3.217</td>\n",
       "      <td>33.201</td>\n",
       "      <td>20.05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 00:29:00</th>\n",
       "      <td>22.307</td>\n",
       "      <td>3.265</td>\n",
       "      <td>33.199</td>\n",
       "      <td>20.08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 00:23:00</th>\n",
       "      <td>22.320</td>\n",
       "      <td>3.220</td>\n",
       "      <td>33.197</td>\n",
       "      <td>20.09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 00:17:00</th>\n",
       "      <td>22.322</td>\n",
       "      <td>3.211</td>\n",
       "      <td>33.199</td>\n",
       "      <td>20.08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 00:11:00</th>\n",
       "      <td>22.323</td>\n",
       "      <td>3.134</td>\n",
       "      <td>33.197</td>\n",
       "      <td>20.09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-11-10 00:05:00</th>\n",
       "      <td>22.315</td>\n",
       "      <td>3.018</td>\n",
       "      <td>33.197</td>\n",
       "      <td>20.09</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     chl (ug/L)  pres (dbar)  sal (PSU)  temp (C)\n",
       "Date                                                             \n",
       "2015-11-10 01:42:00      22.307        3.712     33.199     19.95\n",
       "2015-11-10 01:35:00      22.311        3.588     33.201     19.94\n",
       "2015-11-10 01:29:00      22.305        3.541     33.200     19.95\n",
       "2015-11-10 01:23:00      22.323        3.463     33.200     19.95\n",
       "2015-11-10 01:17:00      22.316        3.471     33.199     19.95\n",
       "2015-11-10 01:11:00      22.315        3.476     33.198     19.95\n",
       "2015-11-10 01:05:00      22.310        3.448     33.199     19.96\n",
       "2015-11-10 00:59:00      22.316        3.377     33.200     19.99\n",
       "2015-11-10 00:53:00      22.311        3.338     33.200     20.00\n",
       "2015-11-10 00:47:00      22.322        3.325     33.201     20.01\n",
       "2015-11-10 00:41:00      22.311        3.344     33.200     20.04\n",
       "2015-11-10 00:35:00      22.311        3.217     33.201     20.05\n",
       "2015-11-10 00:29:00      22.307        3.265     33.199     20.08\n",
       "2015-11-10 00:23:00      22.320        3.220     33.197     20.09\n",
       "2015-11-10 00:17:00      22.322        3.211     33.199     20.08\n",
       "2015-11-10 00:11:00      22.323        3.134     33.197     20.09\n",
       "2015-11-10 00:05:00      22.315        3.018     33.197     20.09"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc['11/10/15']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
